package com.xc.hotelmgr.webapi.modules.redis.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.hotelmgr.webapi.common.utils.Constant;
import com.xc.hotelmgr.webapi.common.utils.DateUtils;
import com.xc.hotelmgr.webapi.common.utils.RedisUtils;
import com.xc.hotelmgr.webapi.modules.redis.dao.RPRedisDao;
import com.xc.hotelmgr.webapi.modules.redis.dto.RPBlongHtlRoom;
import com.xc.hotelmgr.webapi.modules.redis.dto.RPS;
import com.xc.hotelmgr.webapi.modules.redis.dto.StoreRPUseAddPrice;
import com.xc.hotelmgr.webapi.modules.redis.dto.StoreHtlUseOurPlan;
import com.xc.hotelmgr.webapi.modules.redis.service.RPRedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Service("RPRedisService")
public class RPRedisServiceImpl extends ServiceImpl<RPRedisDao, StoreRPUseAddPrice> implements RPRedisService {
    @Autowired
    RedisUtils redisUtils;

    @Override
    public void storeRpSaveUseAddPriceCfg(Integer storeId, String appKey, Long hotelId) {
        //查询酒店加价配置
        List<StoreRPUseAddPrice> htlAddPrices = baseMapper.selectHtlAddPrice(storeId, hotelId);
        Map<String,Object> map = new HashMap<>();
        if(htlAddPrices.isEmpty()){
            //如果没有设置酒店加价配置，则查询自定义价格加价配置
            htlAddPrices = baseMapper.selectOurPlanAddPrice(storeId, hotelId);
        }
        if(htlAddPrices.isEmpty()){
            //如果即没有配置酒店加价配置，有没有配置自定义加价配置则查询是否有通用配置
            htlAddPrices = baseMapper.selectCommAddPrice(storeId, hotelId);
        }
        for (StoreRPUseAddPrice rpUseAddPrice : htlAddPrices){
            map.put(rpUseAddPrice.getRpCode(),rpUseAddPrice.getId());
        }
        String key = Constant.RedisKey.STORE_RP_USE_ADD_PRICE.getValue() + appKey;
        redisUtils.hashSetAll(key,map);
    }

    @Override
    public void storeHtlSaveOurPlan(Integer storeId, String appKey, Long hotelId) {
        List<StoreHtlUseOurPlan> storeRPUseOurPlans = baseMapper.selectOurPlan(storeId, hotelId);
        Map<String,Object> map = new HashMap<>();
        for (StoreHtlUseOurPlan storeRPUseOurPlan : storeRPUseOurPlans){
            map.put(storeRPUseOurPlan.getHotelId(),storeRPUseOurPlan.getOurPlans());
        }

        String key = Constant.RedisKey.STORE_HTL_USE_OURPLAN.getValue() + appKey;
        redisUtils.hashSetAll(key,map);
    }

    @Override
    public void rpStoreHtlRoomSave(Long hotelId) {
        List<RPBlongHtlRoom> rpBlongHtlRooms = baseMapper.selectHtlRoomCode(hotelId);
        Map<String,Object> map = new HashMap<>();
        for (RPBlongHtlRoom rpBlongHtlRoom : rpBlongHtlRooms){
            map.put(rpBlongHtlRoom.getRpCode(),rpBlongHtlRoom.getHtlRoomCode());
        }
        redisUtils.hashSetAll(Constant.RedisKey.RP_HTL_ROOM.getValue(),map);
    }

    @Override
    public void rpsSave(Long hotelId) {
        String curDateStr = DateUtils.format(new Date(),"yyyy-MM-dd");
        List<RPS> rPSs = baseMapper.slectRPS(hotelId,curDateStr);
        Date priceDate = null;
        Date curDate = DateUtils.stringToDate(curDateStr,"yyyy-MM-dd");
        Long expire = 0L;
        redisUtils.setRpsByPipeline(rPSs);
    }

    @Override
    public void delStoreRpSaveUseAddPriceCfg(String appKey, List<Long> rpList ) {
        String key = Constant.RedisKey.STORE_RP_USE_ADD_PRICE.getValue() + appKey;
//        log.info("delStoreRpSaveUseAddPriceCfg,appKey:{},rpList:{}",appKey,rpList);
        String[] rpListStr= new String[rpList.size()];
        for (int i=0;i< rpList.size();i++) {
            rpListStr[i] = rpList.get(i).toString();
        }
        redisUtils.hashDel(key,rpListStr);
    }

    @Override
    public void delRpStoreHtlRoomSave(List<Long> rpList) {
        String key = Constant.RedisKey.RP_HTL_ROOM.getValue();
        String[] rpListStr= new String[rpList.size()];
        for (int i=0;i< rpList.size();i++) {
            rpListStr[i] = rpList.get(i).toString();
        }
        redisUtils.hashDel(key,rpListStr);
    }

    /**
     * 删除数据库中的物理房型
     * @param hotelId
     */
    @Override
    public void deleteDbRoomByHotelId(Long hotelId) {
        //删除数据库中对应的rp
        baseMapper.deleteDbRoomByHotelId(hotelId);
    }

    @Override
    public void deleteDbRpByHotelId(Long hotelId) {
        //删除数据库中对应的rp
        baseMapper.deleteRpByHotelId(hotelId);
    }

    @Override
    public void deleteDbRPSByHotelId(Long hotelId) {
        //删除数据库中对应的rps
        baseMapper.deleteRPSByHotelId(hotelId);
    }

    @Override
    public void deleteDbRPByIds(List<Long> rpList) {
        baseMapper.deleteDbRPByIds(rpList);
    }
}
